from django.http import HttpResponse
from django.shortcuts import render
from .models import Book
from django.core.paginator import Paginator,Page
from django.views.decorators.cache import cache_page
# Create your views here.

def book(request):
    bs = Book.objects.all()
    paginator = Paginator(bs,10)
    page_number = request.GET.get('page')
    pages = paginator.get_page(page_number)
    return render(request,'main/book.html',context={'pages':pages})


def middleware(request):
    return HttpResponse('恭喜你，请求成功')

@cache_page(timeout=40)
def cache(request):
    """
    假如页面不会随时更新，没有必要每次都去执行复杂逻辑
    """
    first_book = Book.objects.first()
    print(f"查询处理复杂逻辑{first_book.title}")
    return HttpResponse('缓存使用 ')